﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gpsys.Entity.MSG;
using Gpsys.Web.Business;
using System.Web.Mvc;

namespace Gpsys.Web.Models
{
    public class WorkflowEntityModel
    {
        public Workflow Workflow { get; set; }
    }

    public class WorkflowModel
    {
        public bool InsertWorkFlow(Workflow wf)
        {
            WorkflowBusiness bs = new WorkflowBusiness();

            return bs.InsertWorkflow(wf);
        }

        public bool UpdateWorkFlow(Workflow wf)
        {
            WorkflowBusiness bs = new WorkflowBusiness();

            return bs.UpdateWorkflow(wf);
        }

        public Workflow GetWorkflow(int Id)
        {
            WorkflowBusiness bs = new WorkflowBusiness();

            return bs.GetWorkflow(Id);
        }


        public void DeleteWorkflow(int Id)
        {
            WorkflowBusiness bs = new WorkflowBusiness();

            bs.DeleteWorkflow(Id);
        }

        private List<Workflow> m_workflows;

        public List<Workflow> Workflows
        {
            get
            {
                if (m_workflows == null)
                {
                    WorkflowBusiness bs = new WorkflowBusiness();
                    m_workflows = bs.GetWorflows();
                }
                return m_workflows;
            }
        }

        public void ValidateWorkflow(ModelStateDictionary modelState, WorkflowEntityModel model)
        {
            if (string.IsNullOrEmpty(model.Workflow.Code))
                modelState.AddModelError("Workflow.Code", "Code could not be empty.");

            if (string.IsNullOrEmpty(model.Workflow.Xml))
                modelState.AddModelError("Workflow.Xml", "Xml could not be empty.");

            WorkflowBusiness bs = new WorkflowBusiness();
            if (bs.CheckCodeUsed(model.Workflow))
            {
                modelState.AddModelError("Workflow.Code", "Code is already been used.");
            }
        }
    }
}